package com.stream.controller;

import com.alibaba.fastjson.JSON;
import com.stream.model.entity.SysOssEntity;
import com.stream.service.SysConfigService;
import com.stream.service.SysOssService;
import com.stream.utils.*;
import com.stream.utils.oss.CloudStorageConfig;
import com.stream.utils.oss.OSSFactory;
import com.stream.utils.validator.ValidatorUtils;
import com.stream.utils.validator.group.AliyunGroup;
import com.stream.utils.validator.group.QcloudGroup;
import com.stream.utils.validator.group.QiniuGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文件上传
 * Created by Stream on 2017/6/22.
 */

@RestController
@RequestMapping("/sys/oss")
public class SysOssController {

    @Autowired
    private SysOssService sysOssService;

    @Autowired
    private SysConfigService sysConfigService;

    private static final String KEY = Constant.CLOUD_STORAGE_CONFIG_KEY;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:oss:all")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表
         Query query = new Query(params);
        List<SysOssEntity> list = sysOssService.queryList(query);
        int total = sysOssService.queryTotal(query);
        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", page);
    }

    /**
     * 云存储配置信息
     */
    @RequestMapping("/config")
    public R config( ){
        CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);

        return R.ok().put("config", config);
    }

    /**
     * 保存云储存配置信息
     */
    @RequestMapping("/saveConfig")
    @RequiresPermissions("sys:oss:all")
    public R saveConfig(@RequestBody CloudStorageConfig config){
        ValidatorUtils.validateEntity(config);

        if (config.getType() == Constant.CloudService.QINIU.getValue()) {
            ValidatorUtils.validateEntity(config, QiniuGroup.class);
        } else if (config.getType() == Constant.CloudService.ALIYUN.getValue()){
            ValidatorUtils.validateEntity(config, AliyunGroup.class);
        }else if (config.getType() == Constant.CloudService.QCLOUD.getValue()){
            ValidatorUtils.validateEntity(config, QcloudGroup.class);
        }

        sysConfigService.updateValueByKey(KEY, JSON.toJSONString(config));
        return R.ok();
    }

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    @RequiresPermissions("sys:oss:all")
    public R uplaod(@RequestParam("file") MultipartFile file)throws Exception{
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        String url = OSSFactory.build().upload(file.getBytes());

        //保存文件信息
        SysOssEntity oss = new SysOssEntity();
        oss.setUrl(url);
        oss.setCreateDate(new Date());
        sysOssService.save(oss);

        return R.ok().put("url", url);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids ){
        sysOssService.deleteBatch(ids);

        return R.ok();
    }
}
